﻿using Business.Service;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WoChu.Autorun.Base.Dao;
using WoChu.Infrastructure.Common;
using WoChu.Infrastructure.Error;
using WoChu.Infrastructure.ModelCommmon;
using WoChu.Infrastructure.Web;

namespace MvcAppWeb.Controllers
{
    public class GoodsInfoController : BaseDataController
    {
        BGoodsCellNo _goodsServer = new BGoodsCellNo();

        public ActionResult GoodsInfoManage()
        {           
            AppUserList model = (AppUserList)Session["UserInfo"];
            return View(model);
        }

        ///<summary>
        ///查询预估订单信息
        ///</summary>
        ///<returns></returns>
        public JsonResult InitJsonResult(Goods model)
        {
            int page = int.Parse(Request["page"].ToString());
            int rows = int.Parse(Request["rows"].ToString());

            PageInfo<Goods, Goods> pageInfo = new PageInfo<Goods, Goods>()
            {
                Condition = model,
                PageIndex = page,
                PageSize = rows,
            };
            _goodsServer.GetPageList(pageInfo);
            return Json(pageInfo);            
        }
       

        ///<summary>
        ///查询预估订单信息
        ///</summary>
        ///<returns></returns>
        public JsonResult GetGoodsDetail(string goodsCode)
        {

            BGoodsCellNo db = new BGoodsCellNo(); 
            var result = db.GetGoodsDetail(goodsCode).ToList();
            Dictionary<string, object> json = new Dictionary<string, object>();
            json.Add("rows", result);
            return Json(json, JsonRequestBehavior.AllowGet);
        }



        /// <summary>
        /// 审核预购订单
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult SaveData(Goods model)
        {
            var ajaxRet = new AjaxResult();
            try
            {                
                model.LASTMODITIME = DateTime.Now;
                model.LASTMODIOPID = this.CurrentUser.USERNAME;

                int ret = 0;
                if (model.GOODSID < 1)     //新增
                    ret = _goodsServer.Insert(model);
                else
                    ret = _goodsServer.Update(model);

                if (ret < 1)
                {
                    new BussinessException("修改失败").Throw();
                }

                ajaxRet.SetSuccess("操作成功");       
            }
            catch (Exception ex)
            {
                _Log.LogError(ex, "PermissionManageController.SaveUser");
                ajaxRet.SetError(ex, "操作失败");
            }
            return Json(ajaxRet);
        }

        public ActionResult ExistsGoodsCode(Goods model)
        {
            bool exists = _goodsServer.ExistsGoodsCode(model);

            ContentResult cr = new ContentResult();
            cr.Content = (!exists).ToString().ToLower();
            return cr;
        }

        public ActionResult GetInventory(Goods model)
        {
            float total = _goodsServer.GetInventory(model);
            ContentResult cr = new ContentResult();
            cr.Content = total.ToString();
            return cr;
        }



        [HttpPost]
        /// <summary>
        /// 审核预购订单
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public string SaveDataBatch()
        {
            string goodsCodes = Request["hiddenGoodsCodeBatch"];
            string customCellNos = Request["CustomCELLNO_batch"];
            string sjson = string.Empty;
            if (goodsCodes != null && goodsCodes != "")
            {

                bool b = false;
                List<Goods> list = new List<Goods>();
                BGoodsCellNo db = new BGoodsCellNo();
                if (goodsCodes.IndexOf(',') > -1)
                {
                    goodsCodes = goodsCodes.Substring(0, goodsCodes.Length - 1);
                    foreach (var code in goodsCodes.Split(','))
                    {
                        Goods goods = new Goods();
                        goods.GOODSCODE = code;
                        goods.CUSTOMCELLNO = customCellNos;
                        list.Add(goods);
                    }
                    b = db.UpdateGoodsCellNoBatch(list);
                }

                if (b)
                {
                    sjson = "{success:true,msg:'修改成功'}";
                }
                else
                {
                    sjson = "{success:true,msg:'修改失败'}";
                }
            }
            else
            {
                sjson = "{success:false,msg:'请选择数据后再批量修改'}";
            }

            return sjson;
        }

    }
}
